//
//  BankTypeViewController.m
//  jinnengExam
//
//  Created by mac on 2020/11/16.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "BankTypeViewController.h"
#import "PBFlatBarButtonItems.h"
#import "JinnengExamViewController.h"
#import "JinnengExamViewController.h"
@interface BankTypeViewController ()

@end

@implementation BankTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [super viewDidLoad];
    self.title = @"题库选择";
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [ColorUtil navigationBarColor:[ColorUtil titleColor] BarSize:bar.frame alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    [bar setTintColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    [self initView];
    
    UIBarButtonItem *otherButton = [PBFlatBarButtonItems Title:@"选   择" Target:self selector:@selector(submit)];
     [self.navigationItem setRightBarButtonItem:otherButton];
    
}

-(void) viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.frame = CGRectMake(0, 0, 0, 0);
    self.tabBarController.tabBar.hidden = YES;
}

-(void) initView{
   [self.view addSubview:self.jznWebView];
    [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.right.equalTo(self.view);
         if (@available(iOS 11.0, *)) {
             make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
             make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
         } else {
             make.top.bottom.equalTo(self.view);
             make.bottom.equalTo(self.view);
         }
     }];
    self.jznWebView.scrollView.bounces = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/chooseBank" type:@"html"];
    [self registerJSFunction];
}

-(void) submit{

    [self.jznWebView evaluateJavaScript:@"choosItem()" completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                   
               }];
}

-(void) registerJSFunction {
    [self.webViewBridge registerHandler:@"toJznExam" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSString *url;
        int time = 1*60;
        switch (self.type) {
            case 1:
                url = [NSString stringWithFormat:@"%@/exam/findTradeDayPaper",[URLUtil hostName]];
                time = 1*60;
                break;
            case 2:
                url = [NSString stringWithFormat:@"%@/exam/findTradeWeekPaper",[URLUtil hostName]];
                time = 10*60;
                break;
            case 3:
                url = [NSString stringWithFormat:@"%@/exam/findTradeMonthPaper",[URLUtil hostName]];
                time = 30*60;
                break;
            default:
                url = [NSString stringWithFormat:@"%@/exam/findTradeDayPaper",[URLUtil hostName]];
                time = 1*60;
                break;
        }
        int tradeId = [data[@"tradeMulu"] intValue];
        NSDictionary *params = @{
            @"tradeId":[NSString stringWithFormat:@"%d",tradeId],
            @"userId":[NSString stringWithFormat:@"%d",self.loginDto.userId]
        };
        
        [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
                if(dataResult.code == 1102){
                   dispatch_async(dispatch_get_main_queue(), ^(){
                       JinnengExamViewController *vc = [[JinnengExamViewController alloc]init];
                       self.hidesBottomBarWhenPushed = YES;
                       vc.paper = [dataResult.data mj_JSONString];
                       vc.time = self.time;
                       vc.type = self.type;
                       vc.tradeId = tradeId;
                       [self.navigationController pushViewController:vc animated:YES];
                  });
               }else {
                   [[UIApplication sharedApplication].delegate.window makeToast:dataResult.data];
               }
           } failure:^(NSError *error) {
               
        }];
        
    }];

}

-(void) findQuestionBankByParent{
    NSString *url = [NSString stringWithFormat:@"%@/exam/serverMuluList" ,[URLUtil hostName]];
   
    [JZNRequestUtil requestUrl:url requestParams:@{} success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            NSString *clear = [NSString stringWithFormat:@"getBankTypeList(%@)",[dataResult.data mj_JSONString]];
            //[self.jznWebView stringByEvaluatingJavaScriptFromString: clear];
            [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                
            }];
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [self findQuestionBankByParent];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
